<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/15 9:31
 */

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use zgldh\QiniuStorage\QiniuStorage;

class ImageController extends Controller
{

    /**
     * 单图上传
     * @param Request $request
     * @return string
     */

    public function imageUpload(Request $request)
    {
        $type = $t = $request->input('type'); //user ,goods,auth
        if (!in_array($type, ['user', 'goods', 'auth'])) {
            return json_encode(['code' => 1, 'msg' => 'type有误']);
        }
        // 判断是否有文件上传
        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $disk = QiniuStorage::disk('qiniu');
            $fileName = md5($file->getClientOriginalName() . time() . rand()) . '.' . $file->getClientOriginalExtension();
            $bool = $disk->put('web/' . $type . '/image_' . $fileName, file_get_contents($file->getRealPath()));
            // 判断是否上传成功
            if ($bool) {
                $path = '/web/' . $type . '/image_' . $fileName;
                return json_encode(['code' => 0, 'msg' => 'ok', 'data' => ['url' => $path]]);
            }
            return json_encode(['code' => 1, 'msg' => '上传失败', 'data' => ['url' => '']]);
        }
        return json_encode(['code' => 1, 'msg' => '没有文件', 'data' => ['url' => '']]);
    }


    /**
     * 多图上传
     * @param Request $request
     * @return string
     */
    public function moreImageUpload(Request $request)
    {
        $type = $request->input('type');//user ,goods,auth
        if (!in_array($type, ['user', 'goods', 'auth'])) {
            return json_encode(['code' => 1, 'msg' => 'type有误']);
        }
        $file = $request->file('file');
        $filePath = [];  // 定义空数组用来存放图片路径
        foreach ($file as $key => $value) {
            // 判断图片上传中是否出错
            if (!$value->isValid()) {
                return json_encode(['code' => 1, 'msg' => '上传图片出错，请重试', 'data' => '']);
            }
            if (!empty($value)) { //此处防止没有多文件上传的情况
                $allowed_extensions = ["png", "jpg", "gif"];
                if ($value->getClientOriginalExtension() && !in_array($value->getClientOriginalExtension(), $allowed_extensions)) {
                    return json_encode(['code' => 1, 'msg' => '您只能上传PNG、JPG或GIF格式的图片！', 'data' => '']);
                }
                $disk = QiniuStorage::disk('qiniu');
                $fileName = md5($value->getClientOriginalName() . time() . rand()) . '.' . $value->getClientOriginalExtension();
                $bool = $disk->put('web/' . $type . '/image_' . $fileName, file_get_contents($value->getRealPath()));
                if ($bool) {
                    $filePath[] = 'web/' . $type . '/image_' . $fileName;
                }
            }
        }
        if (count($filePath)) {
            return json_encode(['code' => 0, 'msg' => '上传ok!', 'data' => ['url' => $filePath]]);
        } else {
            return json_encode(['code' => 1, 'msg' => '上传多张图片!', 'data' => '']);
        }
    }

    public static function uploadUrlImageOne($image_url)
    {
        $disk = QiniuStorage::disk('qiniu');
        $url_ext = getExt($image_url);
        $fileName = md5(time() . rand()) . '.' . $url_ext;
        $path = 'web/user/avatar/image_' . $fileName;
        $bool = $disk->put($path, file_get_contents($image_url));
        if ($bool) {
            return env('IMAGE_HTTP_URL') . $path;
        } else {
            return false;
        }
    }


}